package com.vtuzx.kingdee.xqc_core.dao;

import com.mongodb.client.MongoCollection;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.db.mongo.impl.VtuzxDaoImpl;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_core.constant.BillStatus;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * 即时库存履历Dao
 */
@Component
public class StockTakeTaskDetailBoxDao extends VtuzxDaoImpl {
    /** 集合名 */
    public static final String NAME = "t_sttd_box_";
    /**
     * 构造
     */
    public StockTakeTaskDetailBoxDao() {
        super(NAME);
    }

    /**
     * 新建单据
     *
     * @param _userId 用户ID
     * @param userName 用户名
     * @param doc 数据
     * @return 单据ID
     */
    public ObjectId insertBill(ObjectId _userId, String userName, Document doc) {
        doc.append("_createUserId", _userId);
        doc.append("createUserName", userName);
        doc.append("_updateUserId", _userId);
        doc.append("updateUserName", userName);
        return insertOne(doc);
    }

    /**
     * 审批单据
     *
     * @param _userId 用户ID
     * @param userName 用户名
     * @param billNo 数据编号
     */
    public void audit(ObjectId _userId, String userName, String billNo) {
        updateOne(Criteria.where("billNo").is(billNo),
                new Document("status", BillStatus.Audit.toString())
                        .append("auditTime", new Date())
                        .append("_auditUserId", _userId)
                        .append("auditUserName", userName));
    }

    /**
     * 反审批单据
     *
     * @param billNo 数据编号
     */
    public void unaudit(String billNo) {
        updateOne(Criteria.where("billNo").is(billNo), new Document("status", BillStatus.Save.toString()));
    }

    /**
     * 保存单据
     *
     * @param billNo 单据号
     */
    public void saveBill(boolean isFP, String billNo, Criteria cond, String detailStatus) {
        MongoCollection<Document> coll = this.getCollection(NAME + billNo);
        coll.find(cond.getCriteriaObject()).forEach(item -> {
            Document setDoc = new Document();
            if (isFP) {
                setDoc.append("inventoryDetailStatus", detailStatus);
            } else {
                setDoc.append("detailStatus", detailStatus);
            }
            Document updateDoc = new Document("$set", setDoc);
            coll.updateOne(Criteria.where(IVtuzxConst.Key__id).is(item.getObjectId(IVtuzxConst.Key__id)).getCriteriaObject(), updateDoc);
        });
    }
    /**
     * 保存单据
     *
     * @param billNo 单据号
     * @param status 保存状态
     */
    public void auditBill(String billNo, String detailStatus, String status, ObjectId _userId, String userName) {
        MongoCollection<Document> coll = this.getCollection(NAME + billNo);
        coll.find().forEach(item -> {
            String inventoryStatus = VtuzxUtil.getString(item, "inventoryStatus");
            double bookCount = VtuzxUtil.getDouble(item, "bookCount");
            double count;
            if ("review".equals(inventoryStatus)) {
                count = VtuzxUtil.getDouble(item, "reviewCount");
            } else {
                count = VtuzxUtil.getDouble(item, "count");
            }
            double TPALCount = count - bookCount;
            Document setDoc = new Document("TPALCount", TPALCount).append("detailStatus", detailStatus).append("inventoryStatus", "review");
            Document updateDoc = new Document("$set", setDoc);
            coll.updateOne(Criteria.where(IVtuzxConst.Key__id).is(item.getObjectId(IVtuzxConst.Key__id)).getCriteriaObject(), updateDoc);
        });
    }

    /**
     * 保存单据
     *
     * @param billNo 单据号
     */
    public void unauditBill(String billNo) {
        MongoCollection<Document> coll = this.getCollection(NAME + billNo);
        coll.find().forEach(item -> {
            Document setDoc = new Document("TPALCount", 1);
            Document updateDoc = new Document("$unset", setDoc);
            coll.updateOne(Criteria.where(IVtuzxConst.Key__id).is(item.getObjectId(IVtuzxConst.Key__id)).getCriteriaObject(), updateDoc);
        });
    }



    public Document statBoxCount(String billNo, Criteria cond) {
        List<Document> docList = new ArrayList<>();
        docList.add(new Document("$match", cond.getCriteriaObject()));
        docList.add(new Document("$group",
                new Document("_id", null).append("scanBoxCount", new Document("$sum", 1))));

        Iterator<Document> resItr = this.aggregate(NAME + billNo, docList);
        if (resItr.hasNext()) {
            return resItr.next();
        }
        return null;
    }
}
